package com.sicnustray.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicnustray.jwt.JwtUtil;
import com.sicnustray.common.Result;
import com.sicnustray.entity.User;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Notice;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sicnustray.service.INoticeService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-11-22
 */
@RequiresPermissions("公告管理")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    
        @Resource
        private INoticeService noticeService;

        // 新增或者更新
        @MyLog(value = "新增或者更新公告")
        @PostMapping
        public Result save(@RequestBody Notice notice) {
                User currentUser = JwtUtil.getCurrentUser();
                notice.setUsername(currentUser.getUsername());
                return Result.success(noticeService.saveOrUpdate(notice));
                }

        @MyLog(value = "单个删除公告")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(noticeService.removeById(id));
                }

        @MyLog(value = "批量删除公告")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(noticeService.removeByIds(ids));
                }

        @MyLog(value = "查询所有公告")
        @GetMapping
        public Result findAll() {
                return Result.success(noticeService.list());
                }

        @MyLog(value = "查找最新公告")
        @GetMapping("/maxId")
        public Result MaxId(){
                return Result.success(noticeService.getMaxId());
        }

        @MyLog(value = "获取单个公告")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(noticeService.getById(id));
                }

        @MyLog(value = "分页查询公告")
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String title) {
                QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
                //queryWrapper.orderByDesc("id");
                if (!"".equals(title)) {
                        queryWrapper.like("title", title);
                }

                return Result.success(noticeService.page(new Page<>(pageNum, pageSize), queryWrapper));
                }

        }

